<template>
  <div>
       <!-- 面包屑导航 -->
    <el-breadcrumb separator-class="el-icon-arrow-right">
      <el-breadcrumb-item :to="{ path: '/home' }">首页</el-breadcrumb-item>
      <el-breadcrumb-item>权限管理</el-breadcrumb-item>
      <el-breadcrumb-item>角色列表</el-breadcrumb-item>
    </el-breadcrumb>
    <!--  -->
    <el-row>
      <el-col>
        <el-button @click="addRoleOpen">添加角色</el-button>
      </el-col>
    </el-row>
      <!-- 卡片 -->
    <el-card class="box-card">
        <!-- 表格 -->
      <el-table stripe :data="RolesList" border>
        <!-- 折叠 权限详细 -->
        <el-table-column type="expand">
          <template slot-scope="scope" >
            <!-- 一次循环 -->
            <el-row class="borderBottom vertical" v-for="item1 in scope.row.children" :key="item1.id">
              <el-col :span="5">
                <el-tag @close="closetag(scope.row, item1.id)" closable type="info">{{item1.authName}}</el-tag>
                <i class="el-icon-right"> </i>
              </el-col>
              <el-col :span="19">
                <!-- 二次循环 -->
                <el-row class="borderBottom vertical" v-for="item2 in item1.children" :key="item2.id">
                  <el-col :span="6">
                    <el-tag @close="closetag(scope.row, item2.id)" closable  type="warning">{{item2.authName}}</el-tag>
                    <i class="el-icon-right"></i>
                  </el-col>
                    <!-- 三次循环 -->
                  <el-col :span="18">
                    <el-tag @close="closetag(scope.row, item3.id)" closable type="success" v-for="item3 in item2.children" :key="item3.id">{{item3.authName}}</el-tag>
                  </el-col>
                </el-row>
              </el-col>
            </el-row>
          </template>
        </el-table-column>
        <!-- 序号 -->
        <el-table-column type="index"></el-table-column>
        <el-table-column prop="roleName" label="角色名" ></el-table-column>
        <el-table-column prop="roleDesc" label="负责" ></el-table-column>
        <el-table-column  label="操作">
          <template slot-scope="scope">
               <el-button type="primary" icon="el-icon-edit">编辑</el-button>
               <el-button type="danger" icon="el-icon-delete">删除</el-button>
               <el-button  type="warning" icon="el-icon-setting" @click="getAllRightsTree(scope.row)">分配权限</el-button>
          </template>
        </el-table-column>
      </el-table>
    </el-card>

<!-- 分配权限的对话框 -->
    <el-dialog @close="rightsDialogClosed" title="提示" :visible.sync="isShowSetRightsDialog" width="50%" >
      <!-- 内容  权限树-->
      <el-tree ref="treeRef" :default-checked-keys="defKeys" show-checkbox node-key="id" default-expand-all :data="allRightsTree" :props="treeProps" ></el-tree>
      <span>这是一段信息</span>
      <!-- 尾巴 -->
      <span slot="footer" class="dialog-footer">
        <el-button @click="isShowSetRightsDialog = false">取 消</el-button>
        <el-button type="primary" @click="setRoleRights">确 定</el-button>
      </span>
    </el-dialog>
<!-- 添加用户 -->
    <el-dialog title="提示" :visible.sync="isShowAddRole"  width="50%" >
      <!-- 内容 -->
      <el-form :model="addRoleInfo" :rules="addRoleRules" ref="addRoleForm">
        <!-- 角色 -->
        <el-form-item label="角色名" prop="roleName">
          <el-input type="info" v-model="addRoleInfo.roleName"></el-input>
        </el-form-item>
        <!-- 描述 -->
        <el-form-item label="角色描述" prop="roleDesc">
          <el-input type="info" v-model="addRoleInfo.roleDesc"></el-input>
        </el-form-item>
      </el-form>
      <!-- 尾巴 -->
      <span slot="footer" class="dialog-footer">
        <el-button @click="isShowAddRole = false">取 消</el-button>
        <el-button type="primary" @click="addRole">确 定</el-button>
      </span>
    </el-dialog>
  </div>
</template>
<script>
export default {
  data () {
    return {
      RolesList: [],
      isShowSetRightsDialog: false,
      isShowAddRole: false,
      allRightsTree: [],
      treeProps: {
        label: 'authName',
        children: 'children'
      },
      // 添加用户对话框 信息
      addRoleInfo: {
        roleName: '',
        roleDesc: ''
      },
      // 添加用户 规则
      addRoleRules: {
        roleName: [
          { required: true, message: '输入东西', trigger: 'blur' }
        ],
        roleDesc: [
          { required: true, message: '输入东西', trigger: 'blur' }
        ]
      },
      defKeys: [],
      roleID: ''
    }
  },
  created () {
    this.getRolesList()
  },
  methods: {
    async getRolesList () {
      const { data: res } = await this.$http.get('roles')
      // console.log(res)
      if (res.meta.status !== 200) return this.$Message.error('获取角色列表失败')
      this.RolesList = res.data
      this.$Message.success('获取角色列表成功')
    },
    // 删除第三权限
    async closetag (row, itemid) {
      const confirmResult = await this.$confirm('此操作将永久删除该文件, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).catch(err => err)
      if (confirmResult !== 'confirm') return this.$Message.info('关闭了')
      this.$Message.success('删除了')
      const { data: res } = await this.$http.delete(`roles/${row.id}/rights/${itemid}`)
      row.children = res.data
    },
    // 打开分配权限对话框   权限树
    async getAllRightsTree (role) {
      const { data: res } = await this.$http.get('rights/tree')
      this.roleID = role.id
      // console.log(res)
      // console.log(role)
      if (res.meta.status !== 200) return this.$Message.error('权限树获取失败')
      this.$Message.success('权限树获取成功')
      this.allRightsTree = res.data
      // 递归获取 3级节点的id
      this.getLeafKeys(role, this.defKeys)
      // console.log(this.defKeys)
      this.isShowSetRightsDialog = true
    },
    // 关闭对话框清空 数组
    rightsDialogClosed () {
      this.defKeys = []
    },
    // 打开 添加用户对话框
    addRoleOpen () {
      this.isShowAddRole = true
    },
    // 添加用户
    addRole () {
      this.$refs.addRoleForm.validate(async valid => {
        if (!valid) return this.$Message.error('valid')
        const { data: res } = await this.$http.post('roles', this.addRoleInfo)
        console.log(res)
        this.addRoleInfo = res.data
        if (res.meta.status !== 201) return this.$Message.error('没有创造出来')
        this.$Message.success('成功添加')
        this.isShowAddRole = true
        this.getRolesList()
      })
    },
    // 递归 获取角色3级权限下的id 保存到数组 defKeys 中
    getLeafKeys (node, arr) {
      // console.log(node)
      if (!node.children) {
        // console.log(node)
        return arr.push(node.id)
      }
      node.children.forEach(item => this.getLeafKeys(item, arr))
      // console.log(arr + '?')
    },
    // 点击就分配权限
    async setRoleRights () {
      const keys = [
        ...this.$refs.treeRef.getCheckedKeys(), ...this.$refs.treeRef.getHalfCheckedKeys()
      ]
      console.log(keys)
      console.log(keys[1])
      const idKeys = keys.join(',')
      console.log(idKeys)
      const { data: res } = await this.$http.post(`roles/${this.roleID}/rights`, { rids: idKeys })
      console.log(res)
      if (res.meta.shuatus !== 200) return this.$Message.error('更新权限失败')
      this.$Message.success('更新权限成功')
    }
    // --
  }
}
</script>
<style lang="less" scoped>
  .borderTop {
    border-top: 1px solid #eee;
  }
  .borderBottom {
    border-bottom: 1px solid #eee;
  }
  .vertical {
    display: flex;
    align-items: center;
  }
</style>
